﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;  
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace VSObjectTreeAddIn
{
    public partial class AboutUs : Form
    {
        public AboutUs()
        {
            InitializeComponent();
        }

        private void AboutUs_Load(object sender, EventArgs e)
        {
            Label_Version.Text = "1.00";
            Label_OperateSystem.Text = GetWindowsVersion();// Environment.OSVersion.ToString();

            long memorySize = GetPhisicalMemory();
            double memoryGSize = ((double)memorySize) / (1024 * 1024 * 1024);

            Label_ValidMemory.Text = (Math.Round (memoryGSize ,3)).ToString()+ " G";
        }

        private void Button_OK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LinkLabel_Company_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.f980.net"); 
        }

        public string GetWindowsVersion()
        {                

            string osVersion = string.Empty;
            OperatingSystem os = Environment.OSVersion;
            switch (os.Platform)
            {
                case PlatformID.Win32Windows:
                    switch (os.Version.Minor)
                    {
                        case 0:
                                osVersion = "Windows   95";
                                break;
                        case 10:
                            if (os.Version.Revision.ToString() == "2222A")
                                osVersion = "Windows   98   第二版";
                            else
                                osVersion = "Windows   98";
                            break;
                        case 90:
                            osVersion = "Windows   Me";
                            break;
                    }
                    break;
                case PlatformID.Win32NT:
                    switch (os.Version.Major)
                    {
                        case 3:
                            osVersion = "Windows   NT   3.51";
                            break;
                        case 4:
                            osVersion = "Windows   NT   4.0";
                            break;
                        case 5:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    osVersion = "Windows   200";
                                    break;
                                case 1:
                                    osVersion = "Windows   XP";
                                    break;
                                case 2:
                                    osVersion = "Windows   2003";
                                    break;
                            }
                            break;
                    }
                    break;
            }
            return osVersion +" "+os.ServicePack ;
        }

        private static long GetPhisicalMemory()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象 
            searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件 
            ManagementObjectCollection collection = searcher.Get(); //获取内存容量 
            ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();

            long capacity = 0;
            while (em.MoveNext())
            {
                ManagementBaseObject baseObj = em.Current;
                if (baseObj.Properties["Capacity"].Value != null)
                {
                    try
                    {
                        capacity += int.Parse(baseObj.Properties["Capacity"].Value.ToString());
                    }
                    catch
                    {
                        Console.WriteLine("有错误发生！", "错误信息");
                        return 0;
                    }
                }
            }
            return capacity;
        }   
    }
}
